import { prop, Ref, modelOptions } from '@typegoose/typegoose'
import { LoginProvider } from '@app/constants/thirdparty'
import { User } from '@app/modules/user/user.model'
import { getProviderByTypegooseClass } from '@app/transformers/model.transformer'

@modelOptions({
  schemaOptions: {
    versionKey: false,
  },
})
export class OAuth {
  @prop({ ref: () => User, required: true })
  user!: Ref<User>

  @prop({ required: true, enum: LoginProvider })
  provider!: string

  @prop({ required: true })
  openid!: string

  @prop({ required: true })
  unionid?: string

  @prop({ required: true })
  refresh_token?: string

  @prop({ required: true })
  expires_in?: number

  @prop({ default: Date.now })
  createdAt?: Date

  @prop({ default: Date.now })
  updateAt?: Date
}

export const AuthProvider = getProviderByTypegooseClass(OAuth)
